package com.ymxk.timestar.activity;

import android.animation.Animator;
import android.content.Context;
import android.content.Intent;
import android.graphics.PointF;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.ymxk.timestar.activity.adapter.RecyclerButtonAdapter;
import com.ymxk.timestar.activity.adapter.RecyclerImageAdapter;
import com.ymxk.timestar.view.drag.DragDataEntity;
import com.ymxk.timestar.view.drag.utils.AnimationUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 实现一个RecyclerView的平移动画效果、
 */
public class RecyclerTranslationActivity extends AppCompatActivity {

    private AlertDialog mButtonDialog;
    private ConstraintLayout mMain;
    private TextView mTvViewGroup;
    private ConstraintLayout mClViewGroup;
    private TextView mTvCenter;
    private View mVVerLine;
    private View mVHorizontalLine;
    private RecyclerView mRvImage;
    private final String TAG = RecyclerTranslationActivity.class.getSimpleName();
    private List<PointF> mPointFList = new ArrayList<>();

    public static void jumpTo(Context context) {
        Intent intent = new Intent(context, RecyclerTranslationActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_recycler_translation);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        showButtonDialog();
        initView();
        onInitRecyclerView(mRvImage);
        mRvImage.post(this::onInitPointFList);

    }

    private void showButtonDialog() {
        if (mButtonDialog == null) {
            View view = LayoutInflater.from(this).inflate(R.layout.dialog_translation_view, null);
            RecyclerView recyclerView = view.findViewById(R.id.rv);
            onInitDialogRecyclerView(recyclerView);
            mButtonDialog = new AlertDialog.Builder(this)
                    .setView(view)
                    .create();
        }
        mButtonDialog.show();

    }

    private void onInitDialogRecyclerView(RecyclerView rv) {
        rv.setLayoutManager(new GridLayoutManager(rv.getContext(), 3));
        rv.setHasFixedSize(true);
        RecyclerButtonAdapter mAdapter = new RecyclerButtonAdapter();
        List<DragDataEntity> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add(new DragDataEntity());
        }
        mAdapter.setItems(list);
        rv.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            Toast.makeText(this, "setOnItemClickListener:" + position, Toast.LENGTH_SHORT).show();
            mButtonDialog.dismiss();
            translation(mPointFList.get(position), mRvImage);
        });
    }


    private void initView() {
        mMain = findViewById(R.id.main);
        mTvViewGroup = findViewById(R.id.tvViewGroup);
        mClViewGroup = findViewById(R.id.clViewGroup);
        mTvCenter = findViewById(R.id.tvCenter);
        mVVerLine = findViewById(R.id.vVerLine);
        mVHorizontalLine = findViewById(R.id.vHorizontalLine);
        mRvImage = findViewById(R.id.rvImage);

        mTvViewGroup.setOnClickListener(v -> showButtonDialog());
    }

    private void onInitRecyclerView(RecyclerView rv) {
        rv.setLayoutManager(new GridLayoutManager(rv.getContext(), 3));
        rv.setHasFixedSize(true);
        rv.setNestedScrollingEnabled(false);
        RecyclerImageAdapter mAdapter = new RecyclerImageAdapter();
        List<DragDataEntity> list = new ArrayList<>();
        for (int i = 0; i < 9; i++) {
            list.add(new DragDataEntity());
        }
        mAdapter.setItems(list);
        rv.setAdapter(mAdapter);
        /*mAdapter.setOnItemClickListener((adapter, view, position) -> {

        });*/
    }

    private void translation(PointF pointF, View view) {
        view.animate()
                .translationX(pointF.x)
                .translationY(pointF.y)
                .setListener(new AnimationUtils.AnimationEndListener() {
                    @Override

                    public void onAnimationEnd(@NonNull Animator animation) {
                        int x = mRvImage.getLeft();
                        int y = mRvImage.getTop();
                        Log.e(TAG, "onAnimationEnd: x:" + x + ",y:" + y);
                        //onAnimationEnd: x:-359,y:106
                    }
                })
                .start();

    }

    private void onInitPointFList() {
        mPointFList.clear();
        float centerX = mTvCenter.getLeft()
                + (mTvCenter.getRight() - mTvCenter.getLeft()) / 2F;
        float centerY = mTvCenter.getTop()
                + (mTvCenter.getBottom() - mTvCenter.getTop()) / 2F;
        Log.e(TAG, "onInitPointFList: centerX:" + centerX
                + ",centerY:" + centerY);
        //centerX:540.0,centerY:1006.0/
        int topMargin = ((ConstraintLayout.LayoutParams) mRvImage.getLayoutParams()).topMargin;
        Log.e(TAG, "onInitPointFList: topMargin:" + topMargin);

        float startX = Math.abs(mRvImage.getLeft());
        float startY = mRvImage.getTop();

        int spanCount = ((GridLayoutManager) mRvImage.getLayoutManager()).getSpanCount();
        float childWidth = 0;
        for (int i = 0; i < mRvImage.getChildCount(); i++) {
            View child = mRvImage.getChildAt(i);
            childWidth = child.getWidth();
            int childHeight = child.getHeight();
            Log.e(TAG, "onInitPointFList: childWidth:" + childWidth
                    + "-->" + i);
            //childWidth:600-->8
            int xLength = i % spanCount;
            int yLength = i / spanCount;
            //if (child.getTop() > centerY)
            //startY -= (childHeight / 2F);
            float x = (centerX)
                    + startX
                    - mRvImage.getPaddingLeft()
                    - childWidth / 2F
                    - xLength * childWidth;
            float y = (centerY)
                    - startY
                    - mRvImage.getPaddingTop()
                    - childHeight / 2F
                    - yLength * childHeight
                    - topMargin;
            PointF pointF = new PointF(x, y);
            mPointFList.add(pointF);
        }
//        float x = mRvImage.getHeight()-(Math.abs(mRvImage.getLeft()));
        float x = mRvImage.getLeft();

        //float y = centerY - mRvImage.getTop();
        float y = mRvImage.getTop();
        Log.e(TAG, "onInitPointFList: x:" + x + ",y:" + y);
        //x:-359,y:106
        mPointFList.add(new PointF(0, 0));
    }

}